package zhoug.common.clip;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;

import androidx.activity.ComponentActivity;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import zhoug.common.utils.UriUtils;
import zhoug.logging.Logger;

/**
 * 系统图片裁剪
 *
 * @Author: zhoug
 * @Date: 2022/9/6
 * @Description:
 */
public class SystemClipImageHelper extends BaseClipImageHelper {
    private static final String TAG = ">>>SystemClipImageHelper";
    private int outputX = 300;
    private int outputY = 300;
    /**
     * 宽高比
     */
    private int aspectX = 1;
    private int aspectY = 1;


    public static SystemClipImageHelper with(@NonNull ComponentActivity activity) {
        return new SystemClipImageHelper(activity);
    }

    public static SystemClipImageHelper with(@NonNull Fragment fragment) {
        return new SystemClipImageHelper(fragment);
    }


    protected SystemClipImageHelper(ComponentActivity activity) {
        super(activity);
    }

    protected SystemClipImageHelper(Fragment fragment) {
        super(fragment);
    }


    @Override
    public SystemClipImageHelper setContext(Context context) {
        super.setContext(context);
        return this;
    }

    /**
     * 设置需要裁剪的图片
     *
     * @param inputData
     * @return
     */
    @Override
    public SystemClipImageHelper setInputData(Uri inputData) {
        super.setInputData(inputData);
        return this;
    }

    /**
     * 设置裁剪后输出的图片
     *
     * @param outputData
     * @return
     */
    @Override
    public SystemClipImageHelper setOutputData(Uri outputData) {
        super.setOutputData(outputData);
        return this;
    }

    /**
     * 设置裁剪后输出的图片 的格式
     *
     * @param outputFormat
     * @return
     */
    @Override
    public SystemClipImageHelper setOutputFormat(Bitmap.CompressFormat outputFormat) {
        super.setOutputFormat(outputFormat);
        return this;
    }

    /**
     * 设置裁剪监听
     *
     * @param onImageClipResultListener
     * @return
     */
    @Override
    public SystemClipImageHelper setOnImageClipResultListener(OnImageClipResultListener onImageClipResultListener) {
        super.setOnImageClipResultListener(onImageClipResultListener);
        return this;
    }

    public SystemClipImageHelper setAspect(int aspectX, int aspectY) {
        this.aspectX = aspectX;
        this.aspectY = aspectY;
        return this;
    }

    public SystemClipImageHelper setOutputX(int outputX, int outputY) {
        this.outputX = outputX;
        this.outputY = outputY;
        return this;
    }

    @Override
    public boolean start() {
        if (inputData == null) {
            Logger.e(TAG, "inputData 不能为null");
            return false;
        }
        if (outputData == null) {
            Logger.e(TAG, "outputData 不能为null");
            return false;
        }
        if (context == null) {
            Logger.e(TAG, "context 不能为null");
            return false;
        }
        Logger.d(TAG, "inputData:" + inputData);
        Logger.d(TAG, "outputData:" + outputData);

        try {
            Logger.d(TAG, "inputData:" + inputData);
            Logger.d(TAG, "outputData:" + outputData);
            String miniType = "image/*";
            Intent intent = new Intent("com.android.camera.action.CROP");
            intent.putExtra("crop", true);
            // aspectX aspectY 是宽高的比例
            intent.putExtra("aspectX", aspectX);
            intent.putExtra("aspectY", aspectY);
            // outputX outputY 是裁剪图片宽高
            intent.putExtra("outputX", outputX);
            intent.putExtra("outputY", outputY);
            intent.putExtra("return-data", false);//不返回数据,数据太大会oom
            intent.putExtra("noFaceDetection", true);//没有人脸识别
            intent.putExtra("outputFormat", outputFormat != null ? outputFormat.toString() : null);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); //设置权限
            intent.setDataAndType(inputData, miniType);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputData);

            //需要显示授权
            UriUtils.grantUriPermission(context, intent, outputData);
            mLauncher.launch(intent);
            return true;
        } catch (Exception e) {
            Logger.e(TAG, e.getMessage());
        }
        return false;
    }


}
